package webservice.restful;

import java.io.StringWriter;
import javax.ejb.EJB;
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import org.json.XML;
import session.stateful.ReservationSessionBean;
import util.helper.ReservationList;

@Path("reservationJsonWebService")
public class ReservationJsonWebService {

    @EJB
    ReservationSessionBean reservationSessionBean;

    public ReservationJsonWebService() {
    }

    /*
     * Input is tourist Id and output is list of reservation by this tourist
     */
    @GET
    @Produces("application/json")
    public String getReservationListJson(@PathParam("touristId") String touristId) {
        String jsonString = "";

        try {
            ReservationList list = new ReservationList();
            list.setReservationList(reservationSessionBean.getFnBReservationListByTouristId(Long.valueOf(touristId)));

            JAXBContext jc = JAXBContext.newInstance(ReservationList.class);
            Marshaller marshaller = jc.createMarshaller();
            StringWriter writer = new StringWriter();
            marshaller.marshal(list, writer);

            String xmlString = writer.toString();

            jsonString = XML.toJSONObject(xmlString).toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return jsonString;
    }
}
